package com.tianji.aigc.tools;

import com.tianji.aigc.constant.ToolConstant;
import com.tianji.aigc.tools.result.PrePlaceOrder;
import com.tianji.aigc.tools.result.ToolResultHolder;
import com.tianji.api.client.trade.TradeClient;
import com.tianji.api.dto.trade.OrderConfirmVO;
import com.tianji.common.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @version: 1.0
 * @Author: ljy
 * @description: 预下单ToolCalling
 * @date: 2025-09-03 11:15
 */

@Slf4j
@Component
public class OrderTool {

    @Autowired
    private TradeClient tradeClient;

    @Tool(description = ToolConstant.Tools.PRE_ORDER_BY_ID)
    public PrePlaceOrder prePlaceOrder(@ToolParam(description = ToolConstant.Tool_Param.COURSE_ID) Long id,
                                       ToolContext toolContext) {
        //获取用户ID
        Long userId = (Long) toolContext.getContext().get(ToolConstant.USER_ID);
        UserContext.setUser(userId);

        OrderConfirmVO orderConfirmVO = tradeClient.prePlaceOrder(Arrays.asList(id));
        //类型转换
        PrePlaceOrder prePlaceOrder = PrePlaceOrder.of(orderConfirmVO);

        String requestId = (String) toolContext.getContext().get(ToolConstant.REQUEST_ID);
        ToolResultHolder.put(requestId, "prePlaceOrder", prePlaceOrder);
        return prePlaceOrder;
    }
}
